<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;  
use App\Jobs\Sendmail;
use App\Models\Cxhshop\Trade;
use Illuminate\Support\Facades\Validator;
use App;
use TokenManage;
use Illuminate\Support\Facades\Auth;
use App\Models\Fenbiao;
use App\Models\UserCall;
class IndexController extends Controller
{  
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    public function __construct() 
    {
        //$this->middleware('guest'); 
        
         
    }
    

    public function  test(Fenbiao $fenbiao){  
        echo '<pre>';
        $params =array();
        $fenbiao->add($params);

        // $fenbiao->setUnionAllTable(1583202025,1583288425);
//         $where1 = " channel_time>={$startTime} AND
//             channel_time<{$endTime} AND
//             chat_type={$chatType}
//             ";
// //查询字段
// $select ="  target_uid,b.nickname,
//             SUM(*) AS total_consume,
//             COUNT(*) AS total_call ";

// $lists1 = with(new UserCall)->setUnionAllTable($startTime, $endTime, ['*'], $where1)   // 这里查找分表的约束条件和字段，所满足的结果联合成一张表
//             ->select(DB::raw($select)) //这里可以重新查找字段，注：selectRaw()方法会返回所有表字段
//             ->leftJoin('b_beauty as b ','target_uid','=','b.id') //左联表
//             ->whereRaw($where)     // 这里的条件不可再填分表的字段条件
//             ->groupBy('target_uid')//分组
//             ->paginate(15);        //分页

      
        
        

        
        


        // print_r($t);

        
        // print_r($s);
        //echo 1111111;
    }



    public function index(Request $request,Trade $trade)
    {  


        

        $res = TokenManage::getToken('Hello World');
        dd($res);

        echo Redis::set('name','1');
        exit;

        $api = isset($_GET['path']) ? $_GET['path'] : 'main';
        $api = 'user.admin';
        $time = $this->getApiLimit($api);
        $key = "api_limit_" . $api;

        $rnum =10;
        // echo $num;
        // exit;
        //未过期
        if (Redis::get($key)) {
            $num = Redis::get($key);
            if($num<$rnum){
                $nums = Redis::incr($key);
                echo $nums;
            }else{
                echo '已经被限制';
            }
        //第一次
        }else{
            Redis::incr($key);
            Redis::expire($key,$time);
            echo '第一次';
        }


        // Redis::set('name', 'guwenjie');
        // echo Redis::hset("aisiqu",'members','ss');
        // $values = Redis::get('name');
        // print_r($values);
        //echo '<pre>';
        // $data = $trade::test();
        // return view('welcome',['news'=>$data]);

        //return $this->SetResponse(CodeEnum::数据表单验证失败, $v->messages()->first(),null,$request->logid);


        // if ($v->errors()->count() > 0) {
        //     return $v->messages()->first();
        //     // 
        // }else{
        //     return 1;
        // }
    }
    public function auth(){
        //print_r(Auth::guard('api')->user());
        // echo Auth::guard('web')->check(); 
        // echo Auth::check();
        // echo Auth::check();
        // $s = app(HomeController::class);
    }

    

    //接口设置方法 
    public function getApiLimit($api, $default = '10')
    {
        //接口名称 = 每次请求ms间隔
        $info = [
            'user.info' => '500',
            'user.login' => '1000'
        ];
        return isset($info[$api]) ? $info[$api] : $default;
    }



    public function Dbs(){
         $s = DB::table('area')->get()->toArray();
         $b = DB::table('area')->get()->first();
         $t = $this->object_array($s);
         print_r($t);


    }

    public function view(Request $request)
    {  
   
        

        $name =["name"=>"heal","address"=>"成都"];

        return view('welcome',['name'=>$name]);
        echo   $request->ip();
        print_r($request->post());
        print_r($request->file('file')->getClientSize());
        print_r(OldMiddleware::class);
   
       
        // print_r($s);
        // $file = $request->file('photo');
        // print_r($file);
        // echo 111;
        // //$user = Redis::set('user:profile:','sadd');
        // $this->dispatch(new Sendmail());
        //echo Redis::get('user:profile:');


    }

    public function session(){

        $request->session()->put('key','haha');
        $value = $request->session()->get('key');
        $data = $request->session()->all();
        print_r($value);
        print_r($data);
        exit;
    }


    public function valite(){
        $post = $request->input();
        $rules = [
            'username'=>'required|min:2|max:20',
            'email'=>'required|email'
        ];
        $messages = [
            'required'=>':attribute 为必填项',
            'min'=>':attribute 长度不符合要求',
            'email'=>':attribute 格式错误'
        ];

        $s = [ 'username'=>'用户名称',
            'email'=>'Email'];
        $validator = Validator::make($post,$rules,$messages,$s);

        if($validator->fails()){
            print_r($validator->errors());
            $errors = $validator->errors()->getMessages();
            print_r($errors);
            // foreach($errors as $key=>$value)
            // {
            //     echo $value[0]."<br>";
            // } 
            return $v->messages()->first();
 
        }
    }
        /**
     * 转化数组对象
     * @param array 多维数组 一维数组
     * @return array
     */
    public function object_array($array){  
        if(is_object($array)) {  
            $array = (array)$array;  
        }else if(is_array($array)) {  
            foreach($array as $key=>$value) {  
                 $array[$key] = object_array($value);  
            }  
        }  
        return $array;  
    }

}
